//
//  HMVistorView.swift
//  weibo
//
//  Created by 杨念川 on 16/8/30.
//  Copyright © 2016年 杨念川. All rights reserved.
//

import UIKit

@objc protocol HMVistorViewDelegate: NSObjectProtocol {
    // 定义方法 默认的方法就是必须实现的方法
    func visitorViewWillLogin()
    // 定义注册方法 可选的
    @objc optional func visitorViewWillRegister()
}
class HMVistorView: UIView {
    //定义代理属性
    weak var delegate: HMVistorViewDelegate?
    //定义一个闭包
    //var loginClosure: (() -> ())?
    
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //初始化UI
    private func setupUI() {
        self.backgroundColor = UIColor(white: 255/275, alpha: 1)
        //添加控件到view上
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //iconView布局
        iconView .snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //circleView布局
        circleView .snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        //messageLabel布局
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.width.equalTo(224)
        }
        //registerButton布局
        registerButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.left.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        //loginButton布局
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        //maskIconView布局
        maskIconView.snp_makeConstraints { (make) in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(registerButton)
        }
    }
    //动画
    func startAnimation() {
        //创建基本动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //动画时间
        anim.duration = 20
        //重复次数
        anim.repeatCount = MAXFLOAT
        //旋转到一定的值
        anim.toValue = 2 * M_PI_2
        //切换页面后是否要移除动画
        anim.isRemovedOnCompletion = false
        //给谁添加动画
        circleView.layer.add(anim, forKey: nil)
        
    }
    //给外部提供一个接口
    func visorViewSetVlaue(imageName: String?,message: String?) {
        if imageName == nil {
            messageLabel.text = message;
            startAnimation()
        }else {
             circleView.isHidden = true
            iconView.image = UIImage(named: imageName!)
            messageLabel.text = message
        }
    }

    //MARK-懒加载
    //房子图标
    private lazy var iconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //圆圈
    private lazy var circleView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    //蒙版
    lazy var maskIconView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //message
    private lazy var messageLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 14)
        label.text = "哈哈哈哈哈哈哈哈哈哈哈哈hahhahhhhahhaha哈哈哈哈哈啊哈哈哈哈哈哈哈哈哈哈哈哈"
        // 多行
        label.numberOfLines = 0
        // 居中
        label.textAlignment = .center
        return label
    }()
    //注册按钮
    private lazy var registerButton: UIButton = {
        let btn = UIButton(textColor: UIColor.orange, fontSize: 14)
        btn.setTitle("注册", for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        return btn
    }()
    //登录按钮
    private lazy var loginButton: UIButton = {
        let btn = UIButton(textColor: UIColor.orange, fontSize: 14)
        btn.setTitle("登录", for: .normal)
        btn.setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        btn.addTarget(self, action: #selector(loginButtonClick), for: .touchUpInside)
        return btn
    }()
    //mark- 按钮的监听事件
    @objc private func loginButtonClick() {
        //loginClosure?()
        //执行代理方法
        delegate?.visitorViewWillLogin()
    }
}
